package com.xiaoyang.forum.controller;

import cn.hutool.core.util.StrUtil;
import com.xiaoyang.forum.common.code.BaseController;
import com.xiaoyang.forum.common.code.Result;
import com.xiaoyang.forum.common.code.ResultCode;
import com.xiaoyang.forum.common.util.JWTUtil;
import com.xiaoyang.forum.domain.dto.MessageDTO;
import com.xiaoyang.forum.domain.vo.MessageVO;
import com.xiaoyang.forum.service.IMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author XiaoYang
 * @since 2025-08-23
 */
@RestController
@RequestMapping("/message")
public class MessageController extends BaseController {

    @Autowired
    private IMessageService messageService;

    /**
     * 发送消息
     * @param token
     * @param messageDTO
     * @return
     */
    @PostMapping("/send")
    public Result<Void> sendMessage(@RequestHeader String token,
                                    @RequestBody MessageDTO messageDTO) {

        // 1. 校验用户是否登录
        if (StrUtil.isEmpty(token)) {
            return Result.failed(ResultCode.USER_NO_LOGIN);
        }
        long userId = (long) JWTUtil.getUserIdFromToken(token);
        messageDTO.setSendUserId(userId);

        return toR(messageService.sendMessage(messageDTO));
    }

    /**
     * 获取未读消息数量
     * @param token
     * @return
     */
    @GetMapping("/getUnreadCount")
    public Result<Long> getUnreadCount(@RequestHeader String token) {
        if (StrUtil.isEmpty(token)) {
            return Result.failed(ResultCode.USER_NO_LOGIN);
        }
        long userId = (long) JWTUtil.getUserIdFromToken(token);
        return Result.success(messageService.getUnreadCount(userId));
    }

    /**
     * 获取消息列表
     * @param token
     * @return
     */
    @GetMapping("/toList")
    public Result<List<MessageVO>> list(@RequestHeader String token) {
        if (StrUtil.isEmpty(token)) {
            return Result.failed(ResultCode.USER_NO_LOGIN);
        }

        long receiveUserId = (long) JWTUtil.getUserIdFromToken(token);
        return Result.success(messageService.toList(receiveUserId));
    }

    /**
     * 修改消息状态
     * @param token
     * @param id
     * @return
     */
    @PutMapping("/updateState")
    public Result<Void> updateState(@RequestHeader String token,
                                    Long id) {
        if (StrUtil.isEmpty(token)) {
            return Result.failed(ResultCode.USER_NO_LOGIN);
        }
        long userId = (long) JWTUtil.getUserIdFromToken(token);
        return toR(messageService.updateState(id, userId));
    }

    /**
     * 回复消息
     * @param token
     * @param messageDTO
     * @return
     */
    @PostMapping("/reply")
    public Result<Void> reply(@RequestHeader String token,
                              @RequestBody MessageDTO messageDTO) {
        // 1. 校验用户是否登录
        if (StrUtil.isEmpty(token)) {
            return Result.failed(ResultCode.USER_NO_LOGIN);
        }
        long userId = (long) JWTUtil.getUserIdFromToken(token);
        messageDTO.setSendUserId(userId);

        return toR(messageService.reply(messageDTO));
    }
}
